/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package GA;

import java.nio.ByteBuffer;

/**
 *
 * @author S094182
 */
public class FunctionEnvironment implements IEnvironment{

    @Override
    public int getGeneLength() {
        return 64;
    }

    @Override
    public int getFitnessType() {
        return BinaryGenome.MINIMIZE;
    }

    @Override
    public double evaluateGenome(BinaryGenome gene) {
        if (gene.isFitnessSet())
            return gene.getFitness();
        double x = byteToDouble(gene.getGenome());
        //gene.setFitness(Math.pow(x, 2) + 4*x + 2);
        gene.setFitness(Math.pow(x, 4) -Math.pow(x, 3) - 4*Math.pow(x, 2) + 2*x + 5);
        
        return gene.getFitness();
    }
    
    public Double byteToDouble(byte[] gene) {
        ByteBuffer buf = ByteBuffer.wrap(gene);
        return buf.getDouble();
    }
    
    public byte[] doubleToByte(Double d){
        ByteBuffer buf = ByteBuffer.wrap(new byte[8]);
        buf.putDouble(d);
        return buf.array();
    }
}
